home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / HexEdit 1.21 / ~Project / Source / HexCompare.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-03  |  8.6 KB  |  375 lines  |  [TEXT/CWIE]

  1. /*********************************************************************
  2.  * HexCompare.c
  3.  *
  4.  * Written by Lane Roathe and Jeff Smith
  5.  * Copyright 1995, Ideas From the Deep
  6.  *********************************************************************
  7.  * Ideas:
  8.  *
  9.  * A. Auto spacing of buttons for width of window...
  10.  * B. Add way of changing the offset of either window so differences
  11.  *        can still be found if one word inserted, or something?
  12.  *********************************************************************/
  13.  
  14. #include "HexEdit.h"
  15.  
  16. // variables
  17. short gCompSizeMode=0,gCompTypeMode=0;
  18.  
  19. extern short CompareFlag;
  20. extern WindowPtr CompWind1,CompWind2;
  21. extern unsigned char    gSearchBuffer[256];
  22.  
  23.  
  24. //Actual compare guts here
  25.  
  26. static void PerformTextDifferenceCompare(EditWindowPtr dWin, EditWindowPtr dWin2)
  27. {
  28.     short        ch,ch2,matchIdx;
  29.     long        addr,addr2,matchAddr;
  30.  
  31.     MySetCursor(C_Watch);
  32.  
  33.     // Search in Direction gSearchDir
  34.     // for text gSearchBuffer
  35.  
  36.     if (gSearchDir == 0) {
  37.         addr = dWin->endSel;
  38.         addr2 = dWin2->endSel;
  39.     }
  40.     else {
  41.         addr = dWin->startSel - 1;
  42.         addr2 = dWin2->startSel - 1;
  43.         if (addr < 0)
  44.             return;
  45.         if (addr2 < 0)
  46.             return;
  47.     }
  48.     
  49.     //1 = byte, 2 = words, 4 = longs, ect...
  50.     gSearchBuffer[0]=gCompSizeMode+1;
  51.     if(gCompSizeMode==CM_Long) gSearchBuffer[0] += 1;
  52.  
  53.  
  54.     matchIdx = 0;
  55.     while (1) {
  56.         ch = GetByte(dWin, addr);
  57.         ch2 = GetByte(dWin2, addr2);
  58.         if (ch != ch2) {                //change this if you want compare for similarities
  59.             if (matchIdx == 0) matchAddr = addr;
  60.             ++matchIdx;
  61.             if (matchIdx >= gSearchBuffer[0])
  62.                 goto Success;
  63.             ++addr;
  64.             ++addr2;
  65.             if (addr == dWin->fileSize) {
  66.                 matchIdx = 0;
  67.                 addr = matchAddr;
  68.             }
  69.             else
  70.                 continue;
  71.         }
  72.         else {
  73.             if (matchIdx) {
  74.                 matchIdx = 0;
  75.                 addr = matchAddr;
  76.             }
  77.         }
  78.         if (gSearchDir == 0) {
  79.             ++addr;
  80.             ++addr2;
  81.             if (addr2 == dWin2->fileSize)
  82.                 goto Failure;
  83.             if (addr == dWin->fileSize)
  84.                 goto Failure;
  85.         }
  86.         else {
  87.             --addr;
  88.             --addr2;
  89.             if (addr < 0)
  90.                 goto Failure;
  91.             if (addr2 < 0)
  92.                 goto Failure;
  93.         }
  94.     }
  95.  
  96. Failure:
  97.     SysBeep(1);
  98.     MySetCursor(C_Arrow);
  99.     return;
  100.  
  101. Success:
  102.     SelectWindow((WindowPtr) dWin);
  103.     dWin->startSel = matchAddr;
  104.     dWin->endSel = dWin->startSel + gCompSizeMode + 1;
  105.     if(gCompSizeMode==CM_Long) dWin->endSel += 1;
  106.     ScrollToSelection(dWin, dWin->startSel, true, true);
  107.  
  108.     SelectWindow((WindowPtr) dWin2);
  109.     dWin2->startSel = matchAddr;
  110.     dWin2->endSel = dWin2->startSel + gCompSizeMode + 1;
  111.     if(gCompSizeMode==CM_Long) dWin2->endSel += 1;
  112.     ScrollToSelection(dWin2, dWin2->startSel, true, true);
  113.  
  114.     MySetCursor(C_Arrow);
  115. }
  116.  
  117. //Actual compare guts here too
  118.  
  119. static void PerformTextMatchCompare(EditWindowPtr dWin, EditWindowPtr dWin2)
  120. {
  121.     short        ch,ch2,matchIdx;
  122.     long        addr,addr2,matchAddr;
  123.  
  124.     MySetCursor(C_Watch);
  125.  
  126.     // Search in Direction gSearchDir
  127.     // for text gSearchBuffer
  128.  
  129.     if (gSearchDir == 0) {
  130.         addr = dWin->endSel;
  131.         addr2 = dWin2->endSel;
  132.     }
  133.     else {
  134.         addr = dWin->startSel - 1;
  135.         addr2 = dWin2->startSel - 1;
  136.         if (addr < 0)
  137.             return;
  138.         if (addr2 < 0)
  139.             return;
  140.     }
  141.     
  142.     //1 = byte, 2 = words, 4 = longs, ect...
  143.     gSearchBuffer[0]=gCompSizeMode+1;
  144.     if(gCompSizeMode==CM_Long) gSearchBuffer[0] += 1;
  145.  
  146.     matchIdx = 0;
  147.     while (1) {
  148.         ch = GetByte(dWin, addr);
  149.         ch2 = GetByte(dWin2, addr2);
  150.         if (ch == ch2) {
  151.             if (matchIdx == 0) matchAddr = addr;
  152.             ++matchIdx;
  153.             if (matchIdx >= gSearchBuffer[0])
  154.                 goto Success;
  155.             ++addr;
  156.             ++addr2;
  157.             if (addr == dWin->fileSize) {
  158.                 matchIdx = 0;
  159.                 addr = matchAddr;
  160.             }
  161.             else
  162.                 continue;
  163.         }
  164.         else {
  165.             if (matchIdx) {
  166.                 matchIdx = 0;
  167.                 addr = matchAddr;
  168.             }
  169.         }
  170.         if (gSearchDir == 0) {
  171.             ++addr;
  172.             ++addr2;
  173.             if (addr2 == dWin2->fileSize)
  174.                 goto Failure;
  175.             if (addr == dWin->fileSize)
  176.                 goto Failure;
  177.         }
  178.         else {
  179.             --addr;
  180.             --addr2;
  181.             if (addr < 0)
  182.                 goto Failure;
  183.             if (addr2 < 0)
  184.                 goto Failure;
  185.         }
  186.     }
  187.  
  188. Failure:
  189.     SysBeep(1);
  190.     MySetCursor(C_Arrow);
  191.     return;
  192.  
  193. Success:
  194.     SelectWindow((WindowPtr) dWin);
  195.     dWin->startSel = matchAddr;
  196.     dWin->endSel = dWin->startSel + gCompSizeMode + 1;
  197.     if(gCompSizeMode==CM_Long) dWin->endSel += 1;
  198.     ScrollToSelection(dWin, dWin->startSel, true, true);
  199.  
  200.     SelectWindow((WindowPtr) dWin2);
  201.     dWin2->startSel = matchAddr;
  202.     dWin2->endSel = dWin2->startSel + gCompSizeMode + 1;
  203.     if(gCompSizeMode==CM_Long) dWin2->endSel += 1;
  204.     ScrollToSelection(dWin2, dWin2->startSel, true, true);
  205.  
  206.     MySetCursor(C_Arrow);
  207. }
  208.  
  209.  
  210. // main handler for the compare of the contents of two windows
  211.  
  212. void DoCompare(void)
  213. {
  214.     GrafPtr        oldPort;
  215.     DialogPtr    pDlg;
  216.     Handle         iHandle;
  217.     short         iType;
  218.     Rect         iRect;
  219.     EventRecord    theEvent;
  220.     WindowPeek    frontWindow;
  221.  
  222.     //open the first window
  223. here:
  224.     CompareFlag=1;
  225.     iType = AskEditWindow();
  226.     if(iType==-1) return;        //if Cancel, exit
  227.     //open the second window
  228.     CompareFlag=2;
  229.     iType = AskEditWindow();
  230.     if(iType==-1) {                //if Cancel, go back to first dialog chose
  231.         frontWindow = (WindowPeek) FrontWindow();    //kill first dialog
  232.         CloseEditWindow((WindowPtr) frontWindow);
  233.         goto here;
  234.     }
  235.     //put up dialog and let user interact
  236.  
  237.     GetPort(&oldPort);
  238.     //make dialog
  239.     pDlg = GetNewDialog (131, 0L, (WindowPtr)-1);
  240.     MoveWindow(pDlg,22,gMaxHeight-64+8,true);                //move dialog to 14,400?
  241. //    SizeWindow(pDlg,CompWind1->portRect.right-16,48,true);        //size dialog to 14,400?
  242.     ShowWindow(pDlg);
  243.     SetPort(pDlg);
  244.     //ring around Ok
  245.     GetDialogItem (pDlg, 1, &iType, &iHandle, &iRect );
  246.     PenSize( 3,3 );
  247.     InsetRect( &iRect, -4,-4);
  248.     FrameRoundRect( &iRect, 16,16 );
  249.     //show the contents of the windows
  250.     DrawPage((EditWindowPtr)CompWind1);
  251.     UpdateOnscreen(CompWind1);
  252.     DrawPage((EditWindowPtr)CompWind2);
  253.     UpdateOnscreen(CompWind2);
  254.             
  255.     //handle event processing
  256.     do {
  257.         WaitNextEvent(everyEvent,&theEvent,0L,NULL);
  258.         iType=0;
  259.         if (IsDialogEvent(&theEvent)) {
  260.             DialogSelect(&theEvent, &pDlg, &iType);
  261.         
  262.             if(iType==1) {            //handle find forward here
  263.                 gSearchDir = 0;     //set flag: forward
  264.                 if(gCompTypeMode == CM_Match)
  265.                     PerformTextMatchCompare((EditWindowPtr)CompWind1,(EditWindowPtr)CompWind2);
  266.                 else
  267.                     PerformTextDifferenceCompare((EditWindowPtr)CompWind1,(EditWindowPtr)CompWind2);
  268.                 SelectWindow((WindowPtr) pDlg);
  269.             }
  270.             if(iType==3) {            //handle find backward here
  271.                 gSearchDir = 1;     //set flag: backward
  272.                 if(gCompTypeMode == CM_Match)
  273.                     PerformTextMatchCompare((EditWindowPtr)CompWind1,(EditWindowPtr)CompWind2);
  274.                 else
  275.                     PerformTextDifferenceCompare((EditWindowPtr)CompWind1,(EditWindowPtr)CompWind2);
  276.                 SelectWindow((WindowPtr) pDlg);
  277.             }
  278.         }
  279.     } while ( (iType != 2) && (iType != 4) );    //2 = Done, 4 = Drop out and edit
  280.             
  281.     //close all 3 windows, unless we want to leave them to edit...
  282.     DisposeDialog(pDlg);
  283.     SetPort(oldPort);
  284.     if(iType == 2) {
  285.         frontWindow = (WindowPeek) FrontWindow();
  286.         CloseEditWindow((WindowPtr) frontWindow);
  287.         frontWindow = (WindowPeek) FrontWindow();
  288.         CloseEditWindow((WindowPtr) frontWindow);
  289.     }
  290.     //restore flag
  291.     CompareFlag=false;
  292. }
  293.  
  294. // handler for the options dialog for compare.
  295.  
  296. void DoComparePref(void)
  297. {
  298.     GrafPtr        oldPort;
  299.     DialogPtr    pDlg;
  300.     Handle         iHandle;
  301.     short         iType,radio1,radio2;
  302.     Rect         iRect;
  303.  
  304.     GetPort(&oldPort);
  305.     //make dialog
  306.     pDlg = GetNewDialog (132, 0L, (WindowPtr)-1);
  307.     SetPort(pDlg);
  308.  
  309.     radio1 = gCompSizeMode = gPrefs.compSize;
  310.     radio2 = gCompTypeMode = gPrefs.compType;
  311.  
  312.     //init radio buttons to current settings...
  313.     SetControl(pDlg, CP_Bytes, gCompSizeMode == CM_Byte);
  314.     SetControl(pDlg, CP_Words, gCompSizeMode == CM_Word);
  315.     SetControl(pDlg, CP_Longs, gCompSizeMode == CM_Long);
  316.  
  317.     SetControl(pDlg, CP_Different, gCompTypeMode == CM_Different);
  318.     SetControl(pDlg, CP_Match, gCompTypeMode == CM_Match);
  319.     
  320.     //ring around Ok
  321.     GetDialogItem (pDlg, CP_Done, &iType, &iHandle, &iRect );
  322.     PenSize( 3,3 );
  323.     InsetRect( &iRect, -4,-4);
  324.     FrameRoundRect( &iRect, 16,16 );
  325.             
  326.     //handle event processing
  327.     do {
  328.         ModalDialog(NULL, &iType);
  329.         switch (iType) {
  330.         //handle each radio button...
  331.         case CP_Bytes:
  332.             radio1 = CM_Byte;
  333.             SetControl(pDlg, CP_Bytes, 1);
  334.             SetControl(pDlg, CP_Words, 0);
  335.             SetControl(pDlg, CP_Longs, 0);
  336.             break;
  337.         case CP_Words:
  338.             radio1 = CM_Word;
  339.             SetControl(pDlg, CP_Bytes, 0);
  340.             SetControl(pDlg, CP_Words, 1);
  341.             SetControl(pDlg, CP_Longs, 0);
  342.             break;
  343.         case CP_Longs:
  344.             radio1 = CM_Long;
  345.             SetControl(pDlg, CP_Bytes, 0);
  346.             SetControl(pDlg, CP_Words, 0);
  347.             SetControl(pDlg, CP_Longs, 1);
  348.             break;
  349.             
  350.         case CP_Different:
  351.             radio2 = CM_Different;
  352.             SetControl(pDlg, CP_Different, 1);
  353.             SetControl(pDlg, CP_Match, 0);
  354.             break;
  355.         case CP_Match:
  356.             radio2 = CM_Match;
  357.             SetControl(pDlg, CP_Different, 0);
  358.             SetControl(pDlg, CP_Match, 1);
  359.             break;
  360.         }
  361.     } while ( (iType != CP_Done) && (iType != CP_Cancel) );
  362.     
  363.     if(iType==CP_Done) {
  364.         //change flags based on which one is selected
  365.         gCompSizeMode = radio1;
  366.         gCompTypeMode = radio2;
  367.         //change vars in Prefs...
  368.         gPrefs.compSize = radio1;
  369.         gPrefs.compType = radio2;
  370.     }        
  371.     //close window
  372.     DisposeDialog(pDlg);
  373.     SetPort(oldPort);
  374. }
  375.